package com.xdcplus.web.common.utils;

import com.xdcplus.exceptions.XdcWebException;
import com.xdcplus.mom.enums.ResponseEnum;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuyy
 * @date 2023-04-27-16:13
 */
public class PackDateUtils {

    /**
     * 入参是（2020-01-01）类型的字符串，按照填入天数相加，返回对应的新的日期字符（2020-01-01）
     *
     * @param DateStr  日期字符串
     * @param addCount 新增天数
     * @return 返回新的日期,错误返回null
     */
    public static String getAddDate(String DateStr, int addCount) {

        if (StringUtils.isNotBlank(DateStr)) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = simpleDateFormat.parse(DateStr);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, addCount);
                Date newDate = calendar.getTime();
                return simpleDateFormat.format(newDate);
            } catch (Exception ex) {
                throw new XdcWebException(ResponseEnum.PARAMETER_ERROR);
            }

        }
        return null;
    }

    public static Map<String,String> shift2Time(String shiftDate,String shiftCode){
        LocalDate localDate = LocalDate.parse(shiftDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String startTime;
        String endTime;
        if ("A".equals(shiftCode)) {
            startTime = String.format("%s 08:00:00",shiftDate);
            endTime = String.format("%s 20:00:00",shiftDate);
        }else {
            startTime = String.format("%s 20:00:00",shiftDate);
            endTime = String.format("%s 20:00:00",localDate.plusDays(1)
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        Map<String,String> result = new HashMap<>();
        result.put("startTime",startTime);
        result.put("endTime",endTime);
        return result;
    }


    public static LocalDateTime getShiftStartTime(String shiftDate, String shiftCode){
        LocalDateTime startDateTime = LocalDateTime.parse(shiftDate + " 08:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (!"A".equals(shiftCode)) {
            startDateTime = startDateTime.plusHours(12);
        }
        return startDateTime;
    }


    public static LocalDateTime getShiftEndTime(String shiftDate, String shiftCode){
        LocalDateTime startDateTime = LocalDateTime.parse(shiftDate + " 08:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime eneDateTime;
        if ("A".equals(shiftCode)) {
            eneDateTime = startDateTime.plusHours(12);
        }else {
            eneDateTime = startDateTime.plusDays(1);
        }
        return eneDateTime;
    }


}
